1993C - Light Switches - CodeForces Solution


implementation math

Please click on ads to support us..

Python Code:

def get_input():
    t = int(input())
    cases = []
    for _ in range(t):
        n, k = map(int, input().split(" "))
        a = list(map(int, input().split(" ")))

        cases.append((n, k, a))

    return cases

def get_result(n, k, a):
        m = max(a) + k
        size = 2 * k
        min_start = float("inf")
        max_start = 0
        for i in range(n):
            c = (m - a[i]) // size
            a[i] += size * c

            min_start = min(min_start, a[i])
            max_start = max(max_start, a[i])
            
            if max_start - min_start >= k:
                return -1

        return int(max_start)


def main():
    inputs = get_input()    

    for (n, k, a) in inputs:        
        print(get_result(n, k, a))

if __name__ == '__main__':
    main()
    
    

    


Comments

Submit
0 Comments
More Questions

1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple